<?php
class User_account_model extends CI_Model
{
    private $table   = 'user_account';
    private $table_1 = 'user';

    public function add($postData, $uid)
    {
        $data = array(
            'uid'               => $uid,
            'over_beizhu'       => !empty($postData['over_beizhu']) ? $postData['over_beizhu'] : 0,
            'poundage'          => !empty($postData['poundage']) ? $postData['poundage'] : 0,
            'poundage_rate'     => !empty($postData['poundage_rate']) ? $postData['poundage_rate'] : 0,
            'pay_count_rate'     => !empty($postData['pay_count_rate']) ? $postData['pay_count_rate'] : 0,
            'bargaining'        => !empty($postData['bargaining']) ? $postData['bargaining'] : 0,
            'amount_carry'      => 0,
            'amount_settlement' => 0,
            'amount_bonus'      => !empty($postData['amount_bonus']) ? $postData['amount_bonus'] : 0,
            'amount_month'      => 0,
            'outlay_total'      => 0,
            'income_total'      => 0,
            'rank'              => 0
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function update($postData)
    {
        $data = array(
            'over_beizhu'     => !empty($postData['over_beizhu']) ? $postData['over_beizhu'] : 0,
            'poundage'        => !empty($postData['poundage']) ? $postData['poundage'] : 0,
            'poundage_rate'   => !empty($postData['poundage_rate']) ? $postData['poundage_rate'] : 0,
            'pay_count_rate'     => !empty($postData['pay_count_rate']) ? $postData['pay_count_rate'] : 0,
            'bargaining'      => !empty($postData['bargaining']) ? $postData['bargaining'] : 0,
        );
        $this->db->where('uid', $postData['uid']);
        return  $this->db->update($this->table, $data);
    }
    
    public function delete($id)
    {
        return $this->db->delete($this->table, array('uid'=>$id));
    }
    
    public function findByUid($uid)
    {
        $this->db->where('uid', $uid);
        $result = $this->db->get($this->table);
        if($result->num_rows() > 0){
            return $result;
        }
        return false;
    }
    
    public function updateUserAcount($uid, $account=array())
    {
        $data = array();
        if (!empty($account['amount_month'])) {
            $this->db->set('amount_month', 'amount_month-'.$account['amount_month'], FALSE);
        }
        if (!empty($account['amount_carry'])) {
            $this->db->set('amount_carry', 'amount_carry+'.$account['amount_carry'], FALSE);
        }
        if (!empty($account['amount_bonus'])) {
            $this->db->set('amount_bonus', 'amount_bonus+'.$account['amount_bonus'], FALSE);
        }
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }

    public function findByRechargeTotal($data = array())
    {
        $this->db->select('*');
        $this->db->from($this->table_1.' AS user');
        $this->db->join($this->table.' AS user_account', 'user.uid=user_account.uid', 'inner');
        if (isset($data['userName']) && $data['userName']) {
            $this->db->like('user.user_name', $data['userName']);
            $this->db->or_like('user.alias_name',  $data['userName']);
        }
        if (!empty($data['mobile_phone'])) {
            $this->db->like('user.mobile_phone', $data['mobile_phone']);
        }
        return $this->db->count_all_results();
    }
    
    public function findByRecharge($num, $data = array())
    {
        $this->db->select('*');
        $this->db->from($this->table_1.' AS user');
        $this->db->join($this->table.' AS user_account', 'user.uid=user_account.uid', 'inner');
        if(isset($data['userName']) && $data['userName']){
            $this->db->like('user.user_name', $data['userName']);
            $this->db->or_like('user.alias_name',  $data['userName']);
        }
        if (!empty($data['mobile_phone'])) {
            $this->db->like('user.mobile_phone', $data['mobile_phone']);
        }
        $this->db->limit(20, $num);
        $result = $this->db->get();
        if($result->num_rows() > 0){
            return $result;
        }
        return false;
    }
    
    public function updateUserAccount($uid, $amount_carry)
    {
        $data = array(
            'amount_carry' => $amount_carry,
        );
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }

    /**
     * 用户提现 减少冻结金额
     */
    public function updateWithdraw($uid, $account)
    {
        if (!empty($account['amount_carry']))
        {
            $this->db->set('amount_carry', 'amount_carry+'.$account['amount_carry'], FALSE);
        }

        if (!empty($account['amount_settlement'])) {
            $this->db->set('amount_settlement', 'amount_settlement-'.$account['amount_settlement'], FALSE);
        }

        if (!empty($account['amount_transfer'])) { //待转账金额
            $this->db->set('amount_transfer', 'amount_transfer+'.$account['amount_transfer'], FALSE);
        }

        $this->db->where('uid', $uid);
        return $this->db->update($this->table);
    }

    /**
     * 查找用户的 自动提现的账户余额
     */
    public function findAmountTransfer($uids)
    {
        $this->db->select('user_account.uid, amount_transfer, charge_rate, `bank_id`, `bank_card`, `cardholder`, `pin`, `bank_name`, `user_bank`.`mobile_phone`, user.`user_name`');
        $this->db->from($this->table);
        $this->db->join($this->table_1, 'user.uid = user_account.uid', 'INNER');
        $this->db->join('user_bank', 'user_account.uid = user_bank.uid', 'INNER');
        $this->db->join('user_transfer_charge', 'user_transfer_charge.uid = user_account.uid AND `user_transfer_charge`.`flag` =  1', 'left');
        $this->db->where_in('user_account.uid', $uids);
        $this->db->where('is_default', 1); //扫码账号
        return $this->db->get();
    }
}